package com.itheima.studentManger;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 查看学生");
            System.out.println("2 添加学生");
            System.out.println("3 删除学生");
            System.out.println("4 修改学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int a = sc.nextInt();
            switch (a) {
                case 1:
                    getQueryStudent(list);
                    break;                      //查看学生基本信息
                case 2:
                    getAdd(list);
                    break;                      //添加学生姓名，年龄，学号，身份证号，成绩
                case 3:  getRemove(list);break ;                      //删除学生
                case 4:                         //修改学生基本信息
                case 5:
                    System.out.println("退出成功");
                    break lo;                   //退出界面
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }

    public static void getRemove(ArrayList<Student>list) {
        System.out.println("请输入你要删除的学生学号：");
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int index=getId(list,s);
        if(index==-1){
            System.out.println("输入的学号不存在，请重新退出+");
        }
    }

    //查看学生基本信息
    public static void getQueryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("未录入任何信息，请按2号键录入信息再进行查看");
            return;
        }
        System.out.println("姓名\t年龄\t学号\t身份证号\t成绩");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName() + s.getAge() + s.getStudentid() + s.getIDCard() + s.getScore());
            Scanner sc = new Scanner(System.in);
            System.out.println("按任意键退出查看界面");
            String st = sc.next();
            return;


        }

    }

    //添加学生姓名，年龄，学号，身份证号，成绩
    public static void getAdd(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        lo:
        while (true) {
            while (true) {
                System.out.println("请输入学生姓名");
                String name = sc.next();
                System.out.println("请输入学生年龄");
                int age = sc.nextInt();
                System.out.println("请输入学生学号");
                String studentid = sc.next();
                int index = getId(list, studentid);
                if (index == -1) {
                } else {
                    System.out.println("该学号已被占用，请重新输入：");
                    break;
                }
                System.out.println("请输入学生身份证号");
                String IDCard = sc.next();
                System.out.println("请输入学生成绩");
                String score = sc.next();
                Student s = new Student(name, age, studentid, IDCard, score);
                list.add(s);
                System.out.println("录入成功，是否继续录入，继续录入请按1、退出请按其余数字键");
                int a = sc.nextInt();
                if (a != 1) {
                    System.out.println("退出成功");
                    break lo;
                }
            }

        }
    }

    public static int getId(ArrayList<Student> list, String studentid) {
        int index = -1;//先假设学号不存在；
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);                //获取学号比对，看是否存在
            if (studentid.equals(s.getStudentid())) {
                index = i;
            }
        }
        return index;
    }

}
